package edu.sandau.b230111.b23011125.Service.Impl;
import edu.sandau.b230111.b23011125.Global.GlobalEnum;
import edu.sandau.b230111.b23011125.Model.UserInfo;
import edu.sandau.b230111.b23011125.Model.UserLogin;
import edu.sandau.b230111.b23011125.Repository.UserInfoRepository;
import edu.sandau.b230111.b23011125.Repository.UserLoginRepository;
import edu.sandau.b230111.b23011125.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.UUID;
/*
* 调用了两个
* UserInfoRepository
* UserLoginRepository
* */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserLoginRepository userLoginRepository;
    @Autowired
    private UserInfoRepository userInfoRepository;

    @Override
    public GlobalEnum.OperateResult addUser(UserLogin userLogin) {
        GlobalEnum.OperateResult result = GlobalEnum.OperateResult.UNSUCC;

        UserLogin ans = userLoginRepository.findByAccount(userLogin.getAccount());
        if(ans==null){
            result = GlobalEnum.OperateResult.SUCC;
            String newUUID = UUID.randomUUID().toString();
            userLogin.setUserid(newUUID);
            UserInfo newUserInfo = new UserInfo();
            newUserInfo.setUserid(newUUID);
            newUserInfo.setName("匿名用户");
            newUserInfo.setIsAdmin(0);
            newUserInfo.setSchool("无");
            newUserInfo.setRightCount(0);
            newUserInfo.setSubmitCount(0);
            userLoginRepository.save(userLogin);
            userInfoRepository.save(newUserInfo);
        }
        return result;
    }

    @Override
    public GlobalEnum.OperateResult deleteUser(String userId) {
        GlobalEnum.OperateResult result = GlobalEnum.OperateResult.UNSUCC;
        Optional<UserLogin> ans1 = userLoginRepository.findById(userId);
//        Optional<UserInfo> ans2 = userInfoRepository.findById(userId);
        if(!ans1.isPresent()){ //不存在
            return result;
        }
        result = GlobalEnum.OperateResult.SUCC;
        userLoginRepository.deleteById(userId);
        return result;
    }

    @Override
    public GlobalEnum.OperateResult modifyUserLogin(UserLogin userLogin) {
        GlobalEnum.OperateResult result = GlobalEnum.OperateResult.UNSUCC;
        Optional<UserLogin> ans = userLoginRepository.findById(userLogin.getUserid());
        if(ans.isPresent()){
            result = GlobalEnum.OperateResult.SUCC;
            userLoginRepository.save(userLogin);
        }
        return result;
    }

    @Override
    public GlobalEnum.OperateResult modifyUserInfo(UserInfo userInfo){
        GlobalEnum.OperateResult result = GlobalEnum.OperateResult.UNSUCC;
        Optional<UserInfo> ans = userInfoRepository.findById(userInfo.getUserid());
        if(ans.isPresent()){
            result = GlobalEnum.OperateResult.SUCC;
            userInfoRepository.save(userInfo);
        }
        return result;
    }

    @Override
    public UserLogin queryUserLoginByUserId(String userId) {
        return userLoginRepository.getReferenceById(userId);
    }

    @Override
    public UserLogin queryUserLoginByAccount(String account) {
        return userLoginRepository.findByAccount(account);
    }

    @Override
    public Page<UserLogin> queryAllUserLogin(Integer pageNum,Integer pageSize){
        Pageable page = PageRequest.of(pageNum,pageSize);
        return userLoginRepository.findAll(page);
    }

    @Override
    public Page<UserInfo> queryAllUserInfo(Integer pageNum,Integer pageSize){
        Pageable page = PageRequest.of(pageNum,pageSize);
        return userInfoRepository.findAll(page);
    }

    @Override
    public UserInfo queryUserInfoByUserId(String userId) {
        return userInfoRepository.getReferenceById(userId);
    }

}
